package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.SimpleTreeListParams;
import com.isunland.managesystem.entity.ZTreeNode;
import com.isunland.managesystem.entity.ZTreeNodeListOriginal;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/17 10:16
 */
public class ProjectLedgerQueryFragment extends BaseFragment {
  protected static final String EXTRA_VLAUE =
      ProjectLedgerQueryActivity.class.getSimpleName() + "EXTRA_VLAUE";
  @BindView(R.id.tv_beginprojectSignDate) TextView tvBeginprojectSignDate;
  //@BindView(R.id.ib_beginprojectSignDate) ImageView ibBeginprojectSignDate;
  @BindView(R.id.tv_endprojectSignDate) TextView tvEndprojectSignDate;
  //@BindView(R.id.ib_endprojectSignDate) ImageView ibEndprojectSignDate;
  @BindView(R.id.et_projectName) EditText etProjectName;
  @BindView(R.id.tv_projectKind) TextView tvProjectKind;
  //@BindView(R.id.ib_projectKind) ImageView ibProjectKind;
  @BindView(R.id.et_partaName) EditText etPartaName;

  private int REQUEST_BEGINDATE = 0;
  private int REQUEST_ENDDATE = 1;
  private int REQUEST_KIND = 2;
  private String beginprojectSignDate;//开始时间
  private String endprojectSignDate;//结束时间
  private String projectKindCode;//项目类型code
  private String projectKindName;//项目类型name
  private String projectName;//项目名称
  private String partbDeptName;//往来单位

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.topicQuery);
    ExtraValueContent content = (ExtraValueContent) getArguments().getSerializable(EXTRA_VLAUE);
    if (content == null) return;
    beginprojectSignDate = content.getExtName1();
    endprojectSignDate = content.getExtName2();
    projectName = content.getExtName3();
    projectKindCode = content.getExtCode4();
    projectKindName = content.getExtName4();
    partbDeptName = content.getExtName5();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_project_ledger_query, container, false);
    ButterKnife.bind(this, v);
    bindData();
    return v;
  }

  private void bindData() {
    tvBeginprojectSignDate.setText(beginprojectSignDate);
    tvEndprojectSignDate.setText(endprojectSignDate);
    etProjectName.setText(projectName);
    tvProjectKind.setText(projectKindName);
    etPartaName.setText(partbDeptName);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      //查询
      case R.id.menu_item_confirm:
        volleyPoastSearch();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  //按条件查询订单
  private void volleyPoastSearch() {
    ExtraValueContent content = new ExtraValueContent();
    content.setExtName1(beginprojectSignDate);
    content.setExtName2(endprojectSignDate);
    content.setExtName3(etProjectName.getText().toString());
    content.setExtName4(projectKindName);
    content.setExtCode4(projectKindCode);
    content.setExtName5(etPartaName.getText().toString());

    Intent i = new Intent();
    i.putExtra(EXTRA_VLAUE, content);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  public static ProjectLedgerQueryFragment newInstance(ExtraValueContent content) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_VLAUE, content);
    ProjectLedgerQueryFragment fragment = new ProjectLedgerQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @OnClick({ R.id.ib_beginprojectSignDate, R.id.ib_endprojectSignDate, R.id.ib_projectKind })
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_beginprojectSignDate:
        //noinspection deprecation
        showDialog(
            BaseYMDTimeDialogFragment.newInstance(MyDateUtil.stringToDate(beginprojectSignDate)),
            REQUEST_BEGINDATE);
        break;
      case R.id.ib_endprojectSignDate:
        //noinspection deprecation
        showDialog(
            BaseYMDTimeDialogFragment.newInstance(MyDateUtil.stringToDate(beginprojectSignDate)),
            REQUEST_ENDDATE);
        break;
      case R.id.ib_projectKind:
        //项目类别
        getProjectKind();
        break;
    }
  }

  /** 获取项目类别 */
  public void getProjectKind() {

    SimpleTreeListParams params = new SimpleTreeListParams();
    //noinspection unchecked
    ZTreeNode item = new ZTreeNode();
    item.setName("");
    item.setId("");
    //noinspection unchecked
    params.setItem(item);
    params.setChildField("id");
    params.setParentField("pid");
    params.setTitleField("name");
    params.setTitle("选择项目类型");
    params.setClassOriginal(ZTreeNodeListOriginal.class);
    params.setUrl(ApiConst.URL_QUERY_LIST_PROJECT_TYPE_TREE);
    params.setSelectType(SimpleTreeListParams.ONLY_CHILD);
    params.setParamsNotEmpty(
        new ParamsNotEmpty().put("memberCode", CurrentUser.newInstance(mActivity).getMemberCode())
            .put("dictFlag", "projectKindCode")
            .put("type", "mobile"));
    BaseVolleyActivity.newInstance(this, ZTreeListActivity.class, params, REQUEST_KIND);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_BEGINDATE) {
      Date date = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      beginprojectSignDate = MyDateUtil.format(date, MyDateUtil.TYPE_DATE);
      tvBeginprojectSignDate.setText(beginprojectSignDate);
    }
    if (requestCode == REQUEST_ENDDATE) {
      Date date = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      endprojectSignDate = MyDateUtil.format(date, MyDateUtil.TYPE_DATE);
      tvEndprojectSignDate.setText(endprojectSignDate);
    }
    if (requestCode == REQUEST_KIND) {
      //项目类别
      SimpleTreeListParams params = (SimpleTreeListParams) data.getSerializableExtra(EXTRA_PARAMS);
      ZTreeNode item = ((ZTreeNode) params.getItem());
      projectKindCode = item.getExtParam();
      projectKindName = item.getName();
      tvProjectKind.setText(projectKindName);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
